package com.test.thread;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.concurrent.*;

public class MyThreadPoolDemo {
    public static void main(String[] args) {

        ExecutorService threadPool =  new ThreadPoolExecutor(
                3,
                5,
                1l,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                //new ThreadPoolExecutor.AbortPolicy()); //报异常
                //new ThreadPoolExecutor.CallerRunsPolicy()); //调用者运行机制，任务回退到调用者
                //new ThreadPoolExecutor.DiscardOldestPolicy()); //丢弃等待时间最久的线程
                new ThreadPoolExecutor.DiscardPolicy()); //直接丢弃
        for (int i = 0; i < 20; i++) {
           threadPool.execute(()->{
               System.out.println(Thread.currentThread().getName()+"\t处理业务");
           });
        }
    }

    public static void threadPoolinit(){
        //System.out.println(Runtime.getRuntime().availableProcessors());
        //1.1一池5个线程
        //ExecutorService threadPool = Executors.newFixedThreadPool(5);
        //1.2 一池一个线程
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();
        //1.3 一池n线程
        ExecutorService threadPool = Executors.newCachedThreadPool();


        //模拟10个用户办理业务，每个用户就是一个外部请求
        try{
            for (int i = 0; i < 10; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 办理业务");
                });
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
